package com.fsj.miniapp.service.impl;

import com.fsj.miniapp.entity.Banner;
import com.fsj.miniapp.service.BannerService;
import com.fsj.miniapp.vo.BannerVo;
import com.google.common.collect.Lists;
import com.stip.mybatis.generator.plugin.BaseExample;
import com.stip.mybatis.generator.plugin.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BannerServiceImpl extends BaseService<Banner, BaseExample, Long> implements BannerService {
    @Override
    public List<BannerVo> listFor(Long merchantId) {

        BaseExample example = new BaseExample();

        example.createCriteria().addCriterion("merchant_id=", merchantId);

        example.setOrderByClause(" sorted asc");

        List<Banner> banners = selectByExample(example);
        if (!CollectionUtils.isEmpty(banners)) {
            return banners.stream().map(f -> {
                BannerVo bannerVo = new BannerVo();
                BeanUtils.copyProperties(f, bannerVo);
                return bannerVo;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}